package com.hskn.hss.module.manufacturer.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.entity.ListEntity;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.manufacturer.entity.TlManufacturer;
import com.hskn.hss.module.manufacturer.mapper.TlManufacturerMapper;
import com.hskn.hss.module.manufacturer.service.ITlManufacturerService;
import com.hskn.hss.module.tlequipmodel.mapper.TlEquipmodelMapper;
import com.hskn.hss.module.userdepart.entity.SysUserDepart;
import com.hskn.hss.module.userdepart.mapper.SysUserDepartMapper;
import com.hskn.hss.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-11
 */
@Service
public class TlManufacturerServiceImpl extends ServiceImpl<TlManufacturerMapper, TlManufacturer> implements ITlManufacturerService {

    @Resource
    TlManufacturerMapper tlManufacturerMapper;
    @Resource
    SysUserDepartMapper sysUserDepartMapper;
    @Resource
    TlEquipmodelMapper tlEquipmodelMapper;

    @Override
    public AjaxResult insert(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        TlManufacturer tlManufacturer = params.getObject("data", TlManufacturer.class);

        QueryWrapper<SysUserDepart> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUserDepart::getUserid, uid);
        String deptid = sysUserDepartMapper.selectOne(queryWrapper).getDepid();

        tlManufacturer.setCreatrby(uid);
        tlManufacturer.setCreatetime(new Date());
        tlManufacturer.setDeptid(deptid);
        tlManufacturer.setStatus("1");
        int i = tlManufacturerMapper.insert(tlManufacturer);
        boolean flag = i > 0 ? true : false;

        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult update(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        TlManufacturer tlManufacturer = params.getObject("data", TlManufacturer.class);
        tlManufacturer.setUpdateby(uid);
        tlManufacturer.setUpdatetime(new Date());
        int i = tlManufacturerMapper.updateById(tlManufacturer);
        boolean flag = i > 0 ? true : false;
        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult deletebyid(JSONObject params) throws HssException {
        String id = params.getString("id");
        tlManufacturerMapper.deleteById(id);
        return AjaxResult.success("操作成功");
    }

    @Override
    public TlManufacturer getDetail(JSONObject params) throws HssException {
        String id = params.getString("id");
        TlManufacturer tlManufacturer = tlManufacturerMapper.selectById(id);
        return tlManufacturer;
    }

    @Override
    public List getSelectList(JSONObject params) {
        String status = params == null ? "" : params.getString("status");

        List<Map> resultList = tlManufacturerMapper.getSelectList(status);
        for (Map entity : resultList) {
            Map map = new HashMap();
            map.put("manufacturerid", entity.get("id"));
            map.put("status",status);
            List<ListEntity> modeltList = tlEquipmodelMapper.getSelectList(map);
            entity.put("details", modeltList);
        }
        return resultList;
    }

    @Override
    public JSONObject getList(JSONObject params) throws HssException {
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        JSONObject json = new JSONObject();

        Map<String, Object> map = new HashMap();
        map.put("manufacturername", params.getString("manufacturername"));
        map.put("status", params.getString("status"));
        Page<TlManufacturer> manufacturerPage = new Page<>(page, size, true);
        IPage<TlManufacturer> iPage = tlManufacturerMapper.listmanufacturer(manufacturerPage, map);
        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        json.put("manufacturerlist", iPage.getRecords());
        return json;
    }
}
